import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { getCurrentTime } from '../utils';
import { Response } from '../utils/response';
import { TExperiment } from '../entities/experiment.entity';

@Injectable()
export class ExperimentService {
  constructor(
    @InjectRepository(TExperiment)
    private readonly ExperimentRepository: Repository<TExperiment>,
  ) {}

  // 查询
  async queryListService() {
    try {
      const result = await this.ExperimentRepository.find();
      return Response.success({ data: result });
    } catch (error) {
      return Response.error({ message: error.message });
    }
  }

  // 新增
  async addService(bodyDto) {
    const currentTime = getCurrentTime('YYYY-MM-DD HH:mm:ss');
    const dtoDb = {
      ...bodyDto,
      delFlag: 0,
      createdAt: currentTime,
      updatedAt: currentTime,
    };
    try {
      await this.ExperimentRepository.save(dtoDb);
      return Response.success({});
    } catch (e) {
      return Response.error({ message: e });
    }
  }
}
